package com.fas.core;

import java.util.HashMap;
import java.util.List;

import org.junit.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.dreampie.sqlinxml.SqlKit;

import com.fas.config.AppConfig;
import com.fas.core.constant.OperateType;
import com.fas.hehe.system.model.LoginUser;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.DbKit;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.Table;
import com.jfinal.plugin.activerecord.TableMapping;

/**
 * 
 *
 * @Copyright: Copyright (c) 2015 Knight 
 * @anthor yixiaoping<thinker1013@163.com>
 * @since 2015年10月28日 下午3:17:03
 * @version V1.0
 */
public class BaseService extends Service {
	protected Logger log = LoggerFactory.getLogger(getClass());

	//配置事务拦截器
	public void saveOperateLog(OperateType operateType, Model<?> modelEntity,
			Model<?> oldModelEntity, LoginUser loginUser) {

		Assert.assertNotNull("modelEntity为空", modelEntity);
		Assert.assertNotNull("loginUser为空", loginUser);

		Table table = TableMapping.me().getTable(modelEntity.getClass());
		Assert.assertNotNull("获取Model对应Table为空", table);
		String tableName = table.getName();
		String tableSchema = AppConfig.me().getJdbcSchema();

		String sql1 = SqlKit.sql("base.queryColumnComment");
		List<Record> columnCommontRecords = Db.use(DbKit.MAIN_CONFIG_NAME).find(
				sql1, tableSchema, tableName);
		HashMap<String, String> columnCommonMap = new HashMap<String, String>();
		if (columnCommontRecords != null) {
			for (Record columnComment : columnCommontRecords) {
				columnCommonMap.put(columnComment.getStr("column_name"),
						columnComment.getStr("column_comment"));
			}
		}

		String sql2 = SqlKit.sql("base.queryTableComment");
		Record tableCommontRecord = Db.use(DbKit.MAIN_CONFIG_NAME)
				.findFirst(sql2, tableSchema, tableName);
		String tableComment = tableCommontRecord.getStr("table_comment");

		String[] primaryKey = table.getPrimaryKey();
		Object pkValue = modelEntity.get(primaryKey[0]);
/*
		SysOperateLog operlog = new SysOperateLog();
		Long logid = Sequence.getSequence();
		operlog.set("logid", logid);
		operlog.set("corpid", loginUser.getCorpid());
		operlog.set("account", loginUser.getAccount());
		operlog.set("type", operateType.getCode());
		operlog.set("tablecode", tableName);
		operlog.set("tablename", tableComment);
		operlog.set("opertime", new Date());
		operlog.set("primarykey", pkValue);
		operlog.save();

		String[] attrNames = modelEntity.getAttrNames();
		for (String attrName : attrNames) {
			SysOperateLogContent comment = new SysOperateLogContent();
			comment.set("id", Sequence.getSequence());
			comment.set("logid", logid);
			comment.set("columncode", attrName);
			comment.set("columnname", columnCommonMap.get(attrName));
			comment.set("oldvalue",
					oldModelEntity != null ? oldModelEntity.get(attrName) : "");
			comment.set("newvalue", modelEntity.get(attrName));

			comment.save();
		}*/
	}
	
	public String getLikeValue(Object value){
		return "%"+(String)value+"%";
	}
	
	public Long getLoginCorpid(){
		Long corpid = -1L;
		try{
			LoginUser loginUser = this.getLoginUser();
			corpid = loginUser.getCorpid();
		}catch(Exception e){
			log.error("获取LoginUser为NULL");
		}
		return corpid;
	}
	
}
